﻿using UnityEditor; 

namespace XFGameFramework.MissionSystem
{
    public class ListenerFileEvent : UnityEditor.AssetModificationProcessor
    { 
        public static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions option)
        {
            MissionsConfig missionsConfig = AssetDatabase.LoadAssetAtPath<MissionsConfig>(assetPath);

            if (missionsConfig != null)
            {

                for (int i = 0; i < missionsConfig.missions.Count; i++)
                {
                    if (missionsConfig.missions[i] == null) continue;
                    AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(missionsConfig.missions[i]));
                }

                string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(missionsConfig));
                string dir = string.Format("Assets/Extensions/EquipmentSystem/{0}", guid);
                AssetDatabase.DeleteAsset(dir);
            }

            return AssetDeleteResult.DidNotDelete;
        }

        [UnityEditor.Callbacks.OnOpenAsset(0)]
        private static bool OnOpenAsset(int insId, int line)
        {
            MissionsConfig obj = EditorUtility.InstanceIDToObject(insId) as MissionsConfig;
            if (obj != null)
            {
                MissionConfigInspector.OpenMissionsConfig(obj);
            }
            return obj != null;
        }

    }
}

